home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / ast_comp / modula2.txt < prev    next >
Text File  |  1993-07-04  |  13KB  |  416 lines

  1. From: Cornelia Boldyreff <mips!brunel.ac.uk!Cornelia.Boldyreff>
  2. Date: Fri, 11 May 90 11:55:48 +0100
  3. Message-Id: <11885.9005111055@Saturn.cs.brunel.ac.uk>
  4. To: prls!schutten
  5. Subject: Grammars Request
  6. Status: R
  7.  
  8. This message contains the source of a lex'er and parser for
  9. Modula-2; files m2c.l and m2c.y respectively. The makefile
  10. will generate a syntax checker for Modula 2 in m2c. There is
  11. also a manual page in m2c.1.
  12. Cornelia Boldyreff
  13. p.s. I would be interested to hear what other grammars you receive;
  14. we may be interested in the C/C++ grammars.
  15. ====m2cm.l (=lex input)
  16. %%
  17. [ \n\t]       { ; } /* ignore white space: blanks, newlines & tabs */
  18. AND { return( AND );}
  19. ARRAY { return( ARRAY );}
  20. BEGIN { return( _BEGIN );}
  21. BY { return( BY );}
  22. CASE { return( CASE );}
  23. CONST { return( CONST );}
  24. DEFINITION { return( DEFINITION );}
  25. DIV { return( DIV );}
  26. DO { return( DO );}
  27. ELSE { return( ELSE );}
  28. ELSIF { return( ELSIF );}
  29. END { return( END );}
  30. EXIT { return( EXIT );}
  31. EXPORT { return( EXPORT );}
  32. FOR { return( FOR );}
  33. FROM { return( FROM );}
  34. IF { return( IF );}
  35. IMPLEMENTATION { return( IMPLEMENTATION );}
  36. IMPORT { return( IMPORT );}
  37. IN { return( IN );}
  38. LOOP { return( LOOP );}
  39. MODULE { return( MODULE );}
  40. MOD { return( MOD );}
  41. NOT { return( NOT );}
  42. OF { return( OF );}
  43. OR { return( OR );}
  44. POINTER { return( POINTER );}
  45. PROCEDURE { return( PROCEDURE );}
  46. QUALIFIED { return( QUALIFIED );}
  47. RECORD { return( RECORD );}
  48. REPEAT { return( REPEAT );}
  49. RETURN { return( RETURN );}
  50. SET { return( SET );}
  51. THEN { return( THEN );}
  52. TO { return( TO );}
  53. TYPE { return( TYPE );}
  54. UNTIL { return( UNTIL );}
  55. VAR { return( VAR );}
  56. WHILE { return( WHILE );}
  57. WITH { return( WITH );}
  58. \& { return( ampersand );}
  59. \* { return( asterisk );}
  60. \:\= { return( colon_equals );}
  61. : { return( colon );}
  62. \, { return( comma );}
  63. \.\. { return( dot_dot );}
  64. \. { return( dot );}
  65. = { return( equals_sign );}
  66. \>= { return( greater_than_or_equals );}
  67. \> { return( greater_than );}
  68. \{ { return( left_curley_bracket );}
  69. \( { return( left_parenthesis );}
  70. \[ { return( left_square_bracket );}
  71. \<= { return( less_than_or_equals );}
  72. \<\> { return( not_equals );}
  73. \< { return( less_than );}
  74. \- { return( minus );}
  75. \+ { return( plus );}
  76. # { return( pound_sign );}
  77. \} { return( right_curley_bracket );}
  78. \) { return( right_parenthesis );}
  79. \] { return( right_square_bracket );}
  80. ; { return( semicolon );}
  81. \/ { return( slash );}
  82. \^ { return( up_arrow );}
  83. \| { return( vertical_bar );}
  84. "\""[^"\""\n]*"\"" { return( string );}
  85. \'[^\'\n]*\' { return( string );}
  86. [A-Za-z][0-9A-Za-z]* { return( identifier ); }
  87. [0-9][0-9]*\.[0-9]*E[\+\-][0-9]+ { return( real );}
  88. [0-9][0-9]*\.[0-9]* { return( real );}
  89. [0-7]+B { return( integer );}
  90. [0-7]+C { return( integer );}
  91. [0-9][0-9A-F]*H { return( integer );}
  92. [0-9]+ { return( integer );}
  93. ====m2c.y (=yacc input)
  94. %token AND
  95. %token ARRAY
  96. %token _BEGIN
  97. %token BY
  98. %token CASE
  99. %token CONST
  100. %token DEFINITION
  101. %token DIV
  102. %token DO
  103. %token ELSE
  104. %token ELSIF
  105. %token END
  106. %token EXIT
  107. %token EXPORT
  108. %token FOR
  109. %token FROM
  110. %token IF
  111. %token IMPLEMENTATION
  112. %token IMPORT
  113. %token IN
  114. %token LOOP
  115. %token MOD
  116. %token MODULE
  117. %token NOT
  118. %token OF
  119. %token OR
  120. %token POINTER
  121. %token PROCEDURE
  122. %token QUALIFIED
  123. %token RECORD
  124. %token REPEAT
  125. %token RETURN
  126. %token SET
  127. %token THEN
  128. %token TO
  129. %token TYPE
  130. %token UNTIL
  131. %token VAR
  132. %token WHILE
  133. %token WITH
  134. %token ampersand
  135. %token asterisk
  136. %token colon
  137. %token colon_equals
  138. %token comma
  139. %token dot
  140. %token dot
  141. %token dot_dot
  142. %token end_of_file
  143. %token equals_sign
  144. %token greater_than
  145. %token greater_than_or_equals
  146. %token identifier
  147. %token integer
  148. %token left_curley_bracket
  149. %token left_parenthesis
  150. %token left_square_bracket
  151. %token left_square_bracket
  152. %token less_than
  153. %token less_than_or_equals
  154. %token minus
  155. %token not_equals
  156. %token plus
  157. %token pound_sign
  158. %token real
  159. %token right_curley_bracket
  160. %token right_parenthesis
  161. %token right_square_bracket
  162. %token semicolon
  163. %token slash
  164. %token string
  165. %token up_arrow
  166. %token vertical_bar
  167. %%
  168. compilation : compilation_unit_list ;
  169. compilation_unit_list : compilation_unit | compilation_unit_list
  170.                         compilation_unit;
  171. compilation_unit : module;
  172. module : program_module | definition_module | implementation_module;
  173. file_ident : identifier;
  174. program_module : MODULE file_ident priority_opt semicolon
  175.                  import_list_opt block ident dot;
  176. definition_module : DEFINITION MODULE file_ident semicolon
  177.                     import_list_opt export_opt definition_list_opt
  178.                     END ident dot;
  179. implementation_module : IMPLEMENTATION program_module;
  180. priority_opt : | priority;
  181. priority : left_square_bracket const_expression right_square_bracket;
  182. const_expression : expression;
  183. relation : equals_sign | pound_sign | not_equals | less_than |
  184.            less_than_or_equals | greater_than | greater_than_or_equals
  185.            | IN;
  186. import_list_opt : | import_list;
  187. import_list : import | import_list import;
  188. import : from_opt IMPORT ident_list semicolon;
  189. from_opt : | from;
  190. from : FROM ident;
  191. ident_list : ident | ident_list comma ident;
  192. ident : identifier;
  193. block : declaration_list_opt begin_and_stmts_opt END;
  194. declaration_list_opt : | declaration_list;
  195. declaration_list : declaration | declaration_list declaration;
  196. declaration : error semicolon |
  197.               CONST constant_declaration_list_opt |
  198.               TYPE type_declaration_list_opt |
  199.               VAR variable_declaration_list_opt |
  200.               procedure_declaration semicolon|
  201.               module_declaration semicolon;
  202. begin_and_stmts_opt : | begin_and_stmts;
  203. begin_and_stmts : _BEGIN statement_list;
  204. export_opt : | export;
  205. export : EXPORT qualified_opt ident_list semicolon;
  206. qualified_opt : | qualified;
  207. qualified : QUALIFIED;
  208. definition_list_opt : | definition_list;
  209. definition_list : definition | definition_list definition;
  210. definition : CONST constant_declaration_list_opt |
  211.              TYPE type_definition_list_opt |
  212.              VAR variable_declaration_list_opt |
  213.              procedure_heading semicolon;
  214. type_declaration_list_opt : | type_declaration_list;
  215. type_declaration_list : type_declaration | type_declaration_list
  216.                         type_declaration;
  217. type_declaration : ident equals_sign type semicolon;
  218. constant_declaration_list_opt : | constant_declaration_list;
  219. constant_declaration_list : constant_declaration | constant_declaration_list
  220.                         constant_declaration;
  221. constant_declaration : ident equals_sign const_expression semicolon;
  222. type_definition_list_opt : | type_definition_list;
  223. type_definition_list : type_definition | type_definition_list
  224.                        type_definition;
  225. type_definition : ident type_definition_opt semicolon;
  226. type_definition_opt : | type_definition;
  227. type_definition : equals_sign type;
  228. variable_declaration_list_opt : | variable_declaration_list;
  229. variable_declaration_list : variable_declaration | variable_declaration_list
  230.                         variable_declaration;
  231. variable_declaration : field_declaration semicolon;
  232. field_declaration : ident_list colon type;
  233. proc_ident : ident;
  234. procedure_heading : PROCEDURE proc_ident formal_parameters_opt function_opt;
  235. procedure_declaration : procedure_heading semicolon block ident;
  236. module_declaration : MODULE ident priority_opt semicolon import_list_opt
  237.                      export_opt block ident;
  238. type : simple_type | array_type | record_type | set_type | pointer_type
  239.        procedure_type;
  240. formal_parameters_opt : | formal_parameters;
  241. formal_parameters : left_parenthesis fp_section_list_opt right_parenthesis;
  242. fp_section_list_opt : | fp_section_list;
  243. fp_section_list : fp_section | fp_section_list semicolon fp_section;
  244. fp_section : var_opt ident_list colon formal_type;
  245. function_opt : | function;
  246. function : colon qualident;
  247. var_opt : | var;
  248. var : VAR;
  249. formal_type : array_opt qualident;
  250. qualident : designator;
  251. array_opt : | array;
  252. array : ARRAY OF;
  253. simple_type : qualident | enumeration | subrange_type;
  254. enumeration : left_parenthesis ident_list right_parenthesis;
  255. subrange_type : left_square_bracket subrange right_square_bracket;
  256. subrange : const_expression dot_dot const_expression;
  257. array_type : ARRAY simple_type_list OF type;
  258. simple_type_list : simple_type | simple_type_list comma simple_type;
  259. record_type : RECORD field_list END;
  260. field_list : field | field_list semicolon field;
  261. field :  | field_declaration | variant_field;
  262. variant_field : CASE tag OF variant_list variant_else_opt END;
  263. tag : ident colon qualident | ident ref_list_opt;
  264. variant_list : variant | variant_list vertical_bar variant;
  265. variant : case_label_list colon field_list;
  266. case_label_list : case_label | case_label_list comma case_label;
  267. case_label : const_expression | subrange;
  268. variant_else_opt : | variant_else;
  269. variant_else : ELSE field_list;
  270. set_type : SET OF simple_type;
  271. pointer_type : POINTER TO type;
  272. procedure_type : PROCEDURE formal_type_list_opt function_opt;
  273. formal_type_list_opt : | formal_type_list;
  274. formal_type_list : left_parenthesis ft_list_opt right_parenthesis;
  275. ft_list_opt : | ft_list;
  276. ft_list : ft | ft_list comma ft;
  277. ft : var_opt formal_type;
  278. statement_list : statement | statement_list semicolon statement;
  279. statement :  | assignment | procedure_call | if_statement |
  280.             case_statement | while_statement | repeat_statement |
  281.             loop_statement | for_statement | with_statement | EXIT |
  282.             RETURN expression_opt;
  283. expression_opt : | expression;
  284. assignment : out_designator out_colon_equals expression;
  285. out_designator : designator;
  286. out_colon_equals : colon_equals;
  287. designator : ident ref_list_opt;
  288. ref_list_opt : | ref_list;
  289. ref_list : ref | ref_list ref;
  290. ref : dot ident | left_square_bracket exp_list right_square_bracket |
  291.       up_arrow;
  292. exp_list_opt : | exp_list;
  293. exp_list : expression | exp_list comma expression;
  294. procedure_call : designator actual_parameters_opt;
  295. actual_parameters_opt : | actual_parameters;
  296. actual_parameters : left_parenthesis exp_list_opt right_parenthesis;
  297. if_statement : IF expression THEN statement_list elsif_list_opt
  298.                else_opt END;
  299. elsif_list_opt : | elsif_list;
  300. elsif_list : elsif | elsif_list elsif;
  301. elsif : ELSIF expression THEN statement_list;
  302. else_opt : | else;
  303. else : ELSE statement_list;
  304. case_statement : CASE expression OF case_list else_opt END;
  305. case_list : case | case_list vertical_bar case;
  306. case : case_label_list colon statement_list;
  307. while_statement : WHILE expression DO statement_list END;
  308. repeat_statement : REPEAT statement_list UNTIL expression;
  309. loop_statement : LOOP statement_list END;
  310. for_statement : FOR ident colon_equals expression TO expression by_opt
  311.                 DO statement_list END;
  312. by_opt : | by;
  313. by : BY const_expression;
  314. with_statement : WITH designator DO statement_list END;
  315. expression : simple_expression | simple_expression relation simple_expression;
  316. simple_expression : sign_opt term | simple_expression add_operator term;
  317. add_operator : plus | minus | OR;
  318. sign_opt : | sign;
  319. sign : plus | minus;
  320. term : factor | term mul_operator factor;
  321. mul_operator : asterisk | slash | DIV | MOD | AND | ampersand;
  322. factor : number | string | set | designator actual_parameters_opt |
  323.          left_parenthesis expression right_parenthesis | NOT factor;
  324. number : integer | real;
  325. set : qualident_opt left_curley_bracket element_list_opt
  326.       right_curley_bracket;
  327. qualident_opt : | qualident;
  328. element_list_opt :  | element_list;
  329. element_list : element | element_list comma element;
  330. element : case_label;
  331. %%
  332. #include <stdio.h>
  333. char *filename="-";
  334. main(argc, argv)
  335. int argc;
  336. char *argv[];
  337. {
  338.   register int rc=0;
  339.   extern int yynerrs;
  340.   extern int yylineno;
  341.   if (argc <= 1)
  342.       yyparse();
  343.   else
  344.      {
  345.        while (argc > 1)
  346.        { if (freopen(argv[1], "r", stdin)==NULL) {
  347.              fprintf(stderr, "m2c: %s: cannot open\n", argv[1]);
  348.              rc++; }
  349.          else {
  350.              filename=argv[1];
  351.              yylineno=1;
  352.              yyparse(); }
  353.          argc--; argv++;
  354.        }
  355.       }
  356.    if (yynerrs > 0) rc++;
  357.    return(rc);
  358. }
  359. #include "lex.yy.c"
  360. yyerror(s)
  361.  char *s;
  362. {
  363.  fprintf(stderr, "\"%s\", line %d: %s\n",
  364.          filename, yylineno, s);
  365. }
  366. ====makefile
  367. m2c : m2c.l m2c.y
  368.         lex m2c.l; yacc m2c.y; cc y.tab.c -ll -o m2c
  369. ====m2c.1 (=man page)
  370. .\" $Header: m2c.1, 87/05/26 corn Stab $
  371. .de SB
  372. .\" SuBheader
  373. .sp 1
  374. .nr Sf \\n(.f
  375. .ft B
  376. .PP
  377. \\$1 \\$2 \\$3 \\$4 \\$5 \\$6 \\$7 \\$8 \\$9
  378. .ft \\n(Sf
  379. .sp 1
  380. ..
  381. .TH M2C I
  382. .ad
  383. .SH NAME
  384. m2c \- Modula 2 Syntax Checker
  385. .SH SYNOPSIS
  386. \fBm2c\fP [file...]
  387. .SH DESCRIPTION
  388. .PP
  389. .PP
  390. .PP
  391. .PP
  392. \fIm2c\fP accepts any number of file arguments.
  393. .PP
  394. .SH "SEE ALSO"
  395. .PD 0
  396. .br
  397. .nf
  398. .fi
  399. .PP
  400. .SH KNOWN BUGS/BODGES/LIMITATIONS
  401. .PP
  402. m2c does not always recover from syntax errors.
  403. .PP
  404. Feel free to distribute m2c further but please acknowledge use/references.
  405. .PP
  406. .SH AUTHOR
  407. .PP
  408. .nf
  409. Cornelia Boldyreff
  410. Department of Computer Science
  411. Brunel University
  412. Uxbridge UB8 3PH
  413. The United Kingdom
  414. .br
  415.  
  416.